package com.xhu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhu.common.request.Page;
import com.xhu.common.request.PageWithEmpNameReq;
import com.xhu.common.vo.chance.manage.vo.ChanceManageDistributeVo;
import com.xhu.common.vo.chance.manage.vo.ChanceManageInsertVo;
import com.xhu.pojo.ChanceManage;
import com.xhu.service.ChanceManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/chancemanage")
public class ChanceManageController extends MyBaseController {

    @Autowired
    ChanceManageService service;

    /**
     * 所有分页查询
     */
    @PostMapping("/getall")
    public String getAll(@RequestBody Page page) {
        IPage<ChanceManage> pageRes = service.getAllByPage(page.getCurrent(), page.getSize());

        return this.returnPages(pageRes);
    }

    /**
     * 通过员工名查询并返回
     */
    @PostMapping("/getAllByEmpName")
    public String getAllByEmpName(@RequestBody PageWithEmpNameReq req) {
        IPage<ChanceManage> res = service.getAllByEmpNameToPage(req.getEmpName(), req.getCurrent(), req.getSize());

        return this.returnPages(res);
    }

    /**
     * 通过id删除
     */
    @PostMapping("/deletebyid")
    public String deleteById(@RequestBody String jsonId) {
        String id = this.getSingleStr(jsonId, "id");

        service.deleteById(id);

        return this.returnSuccessWithNoData("删除成功");
    }

    /**
     * 通过id更新
     */
    @PostMapping("/updateonbyid")
    public String updateOneById(@RequestBody ChanceManage entity, HttpServletRequest request) {
        service.updateOneById(entity, request);

        return this.returnSuccessWithNoData("修改成功");
    }

    /**
     * 新增一条
     */
    @PostMapping("/insertone")
    public String insertOne(@RequestBody ChanceManageInsertVo voEntity) {
        service.insertOne(voEntity);

        return this.returnSuccessWithNoData("插入成功");
    }

    /**
     * 通过id获取某一跳消息的内容
     */
    @PostMapping("/getsinglechanceinfo")
    public String getSingleChanceInfo(@RequestBody String jsonId) {
        String id = this.getSingleStr(jsonId, "id");
        ChanceManageDistributeVo res = service.getSingleChanceInfo(id);

        return this.returnSuccessWithData("查询成功", res);
    }

    @PostMapping("/distribute")
    public String distribute(@RequestBody ChanceManageDistributeVo vo){
        service.distribute(vo);

        return this.returnSuccessWithNoData("分配成功");
    }
}
